package ReadCommit;


import ReadCommit.connection.ConnectionInfo;
import ReadCommit.connection.ReadCommitSql;
import ReadCommit.connection.ReadCommitThread;
import ReadCommit.connection.SelectThread;

import java.io.IOException;
import java.sql.*;


public class HalfReadCheckDemo implements ReadCommitSql {

    public void initTableWithoutDrop(ConnectionInfo connectionInfo) throws SQLException {

        try ( Connection connection = DriverManager.getConnection(connectionInfo.getUrl(), connectionInfo.getUsername(), connectionInfo.getPassword());
            Statement statement = connection.createStatement();){
            statement.executeUpdate("delete from account where 1 = 1; insert into account(id, transaction_id, balance) values (0, 1, 0), (1, 2, 100);");
            System.out.println("create table succeed!");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public void initTable(ConnectionInfo connectionInfo) throws SQLException {

        try (Connection connection = DriverManager.getConnection(connectionInfo.getUrl(), connectionInfo.getUsername(), connectionInfo.getPassword());
            Statement statement = connection.createStatement()){
            connection.setAutoCommit(false);
            statement.executeUpdate(initTableSql);
            statement.executeUpdate(initDataSql);
            System.out.println("create table succeed!");
            connection.commit();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void solve(ConnectionInfo connectionInfo, int writeTransNum, int readTransNum) throws SQLException, InterruptedException, IOException {
        initTable(connectionInfo);
        for (int i = 0 ; i < writeTransNum; i++) {
            Thread t = new ReadCommitThread(connectionInfo);
            Thread.sleep(200);
            t.start();
            Thread tSelect = new SelectThread(connectionInfo);
            tSelect.start();
        }
        Thread.sleep(2000);
        for (int i = 0; i <readTransNum; i++) {
            Thread tSelect = new SelectThread(connectionInfo);
            tSelect.start();
            Thread.sleep(150);
        }
    }

    public static void main(String[] args) {
        // url = "jdbc:opengauss://" + args[0] + "192.168.0.28:4000/sharding_db?loggerLevel=OFF";
        if (args.length != 5) {
            System.err.println("need four params >> host:port/databaseName, username, password, amount of write transaction, amount of read transaction");
            return;
        }
        String url = "jdbc:opengauss://" + args[0] + "?loggerLevel=OFF";
        String username = args[1];
        String password = args[2];
        ConnectionInfo connectionInfo = new ConnectionInfo(url, username, password);
        int writeTransNum = Integer.parseInt(args[3]);
        int readTransNum = Integer.parseInt(args[4]);
        try {
            new HalfReadCheckDemo().solve(connectionInfo, writeTransNum, readTransNum);
        } catch (SQLException | InterruptedException | IOException e) {
            throw new RuntimeException(e);
        }
    }
}
